package android.slc.utils;

import android.slc.query.entity.QDateInfo;
import io.reactivex.rxjava3.annotations.NonNull;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具
 * 包括时间格式化等等工具
 */
public class SlcTimeUtils {

    public final static String Y = "yyyy";
    public final static String FULL = "yyyy-MM-dd HH:mm:ss";
    public final static String Y_MO_D_H_M = "yyyy-MM-dd HH:mm";
    public final static String Y_MO_D = "yyyy-MM-dd";
    public final static String Y_MO = "yyyy-MM";
    public final static String MO_D = "MM-dd";
    public final static String MO_D_H_M = "MM-dd HH:mm";
    public final static String H_M_S = "HH:mm:ss";
    public final static String H_M = "HH:mm";

    public final static String ZH_Y = "yyyy年";
    public final static String ZH_FULL = "yyyy年MM月dd日 HH时mm分ss秒";
    public final static String ZH_FULL_EN_TIME = "yyyy年MM月dd日 HH:mm:ss";
    public final static String ZH_Y_MO_D_H_M = "yyyy年MM月dd日 HH时mm分";
    public final static String ZH_Y_MO_D_H_M_EN_TIME = "yyyy年MM月dd日 HH:mm";
    public final static String ZH_Y_MO_D = "yyyy年MM月dd日";
    public final static String ZH_Y_MO = "yyyy年MM月";
    public final static String ZH_MO_D = "MM月dd日";
    public final static String ZH_MO_D_H_M = "MM月dd日 HH时mm分";
    public final static String ZH_MO_D_H_M_EN_TIME = "MM月dd日 HH:mm";
    public final static String ZH_H_M_S = "HH时mm分ss秒";
    public final static String ZH_H_M = "HH时mm分";

    public final static int VALUE_MINUTE_WITH_SECOND = 60;//1分钟/秒单位
    public final static int VALUE_MINUTE_WITH_MILLIS = 60 * 1000;//一分钟/毫秒单位
    public final static int VALUE_HOUR_WITH_MILLIS = VALUE_MINUTE_WITH_MILLIS * 60;
    public final static int VALUE_HOUR_WITH_MINUTE = 60;//1小时/分单位
    public final static int VALUE_DAY_WITH_MILLIS = VALUE_HOUR_WITH_MILLIS * 24;

    /**
     * 清除日历
     *
     * @param calendar
     * @param field
     */
    public static void clearCalendarField(Calendar calendar, int... field) {
        if (calendar != null && field != null) {
            for (int item : field) {
                calendar.set(item, 0);
            }
        }
    }

    public static long clearCalendarField(long timeInMillis, int... field) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeInMillis);
        clearCalendarField(calendar, field);
        return calendar.getTimeInMillis();
    }

    /*--------------------------日期和LocalDate互转 start-----------------------------*/

    public static LocalDate milliseconds2LocalDate(long milliseconds) {
        Date date = new Date();
        date.setTime(milliseconds);
        return date2LocalDate(date);
    }

    public static LocalDate date2LocalDate(Date date) {
        if (date == null) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime milliseconds2LocalDateTime(long milliseconds) {
        Date date = new Date();
        date.setTime(milliseconds);
        return date2LocalDateTime(date);
    }

    public static LocalDateTime date2LocalDateTime(Date date) {
        if (date == null) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static Date localDate2date(LocalDate localDate) {
        if (localDate == null) {
            return null;
        }
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date localDateTime2date(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Long localDate2Millisecond(LocalDate localDate) {
        if (localDate == null) {
            return null;
        }
        //return localDateTime.atZone(ZoneId.of("Asia/Shanghai")).toInstant().getEpochSecond();
        return localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static Long localDateTime2Millisecond(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        //return localDateTime.atZone(ZoneId.of("Asia/Shanghai")).toInstant().getEpochSecond();
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 校验时间是否在指定天数之前
     *
     * @param localDate
     * @param day
     * @return
     */
    public static boolean checkTimeLaterTargetDay(@NonNull LocalDate localDate, int day) {
        return checkTimeLaterTargetDay(localDate.atStartOfDay(), day);
    }

    /**
     * 校验时间是否在指定天数之前
     *
     * @param localDateTime
     * @param day
     * @return
     */
    public static boolean checkTimeLaterTargetDay(@NonNull LocalDateTime localDateTime, int day) {
        LocalDateTime currentLocalDateTime = LocalDateTime.now().minusDays(day);
        return currentLocalDateTime.isBefore(localDateTime);
    }

    /*--------------------------日期和LocalDate互转 end-----------------------------*/

    /*--------------------------QDateInfo相关 start-----------------------------*/

    /**
     * 字符串年月转int
     *
     * @param yearMonth
     * @return
     */
    public static QDateInfo strYM2Int(String yearMonth) {
        String year = yearMonth.split("-")[0];
        String month = yearMonth.split("-")[1];
        QDateInfo qDateInfo = new QDateInfo();
        qDateInfo.setYear(Integer.parseInt(year));
        qDateInfo.setMonth(Integer.parseInt(month));
        return qDateInfo;
    }

    /**
     * 字符串年月日转int
     *
     * @param yearMonthDay
     * @return
     */
    public static QDateInfo strYMD2Int(String yearMonthDay) {
        QDateInfo qDateInfo = strYM2Int(yearMonthDay);
        String day = yearMonthDay.split("-")[2];
        qDateInfo.setDay(Integer.parseInt(day));
        return qDateInfo;
    }

    /*--------------------------QDateInfo相关 end-----------------------------*/

    /**
     * 时间转int数值
     *
     * @param time
     * @return
     */
    public static int[] hourStr2IntArray(String time) {
        String[] timeStrArray = time.split(":");
        int[] intArray = new int[3];
        switch (timeStrArray.length) {
            case 3:
                intArray[2] = Integer.parseInt(timeStrArray[2]);
            case 2:
                intArray[1] = Integer.parseInt(timeStrArray[1]);
            case 1:
                intArray[0] = Integer.parseInt(timeStrArray[0]);
                break;
        }
        return intArray;
    }

    /**
     * 毫秒转小时
     *
     * @param millisecond
     * @return
     */
    public static float millisecond2Hour(long millisecond) {
        return millisecond / (float) VALUE_HOUR_WITH_MILLIS;
    }

    /**
     * 小时转毫秒
     *
     * @param hour
     * @return
     */
    public static long hour2Millisecond(float hour) {
        return (long) (hour * VALUE_HOUR_WITH_MILLIS);
    }

    /**
     * 分钟转小时
     *
     * @param minute
     * @return
     */
    public static float minute2Hour(int minute) {
        return (float) minute / VALUE_HOUR_WITH_MINUTE;
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 获取程序启动时间
     */
    public static Date getJavaStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

}
